package com.wing.device.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.device.dao.DeviceTypeSpecDao;
import com.wing.device.model.entity.DeviceTypeSpec;
import com.wing.device.model.entity.DeviceTypeSpecValue;
import com.wing.device.model.request.DeviceTypeSpecForm;
import com.wing.device.model.request.DeviceTypeSpecValueForm;
import com.wing.device.model.response.DeviceTypeSpecVO;
import com.wing.product.model.entity.ProductSpec;
import com.wing.product.model.entity.ProductSpecValue;
import com.wing.product.model.request.ProductSpecForm;
import com.wing.product.model.request.ProductSpecValueForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 设备类型规格-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class DeviceTypeSpecServiceImpl extends ServiceImpl<DeviceTypeSpecDao, DeviceTypeSpec> implements DeviceTypeSpecService {

    @Autowired
    private DeviceTypeSpecValueService deviceTypeSpecValueService;

    @Override
    public int add(DeviceTypeSpecForm deviceTypeSpecForm) {

        //校验

        DeviceTypeSpec deviceTypeSpec =new DeviceTypeSpec();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(DeviceTypeSpec.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(deviceTypeSpecForm, deviceTypeSpec, copyOptions);

        int insertCount=baseMapper.insert(deviceTypeSpec);
        deviceTypeSpecForm.setId(deviceTypeSpec.getId());
        saveDetail(deviceTypeSpecForm);
        return insertCount;
    }



    @Override
    public List<DeviceTypeSpecVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<DeviceTypeSpecVO> pageList4app(Page page, Map<String, Object> params) {
        List<DeviceTypeSpecVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public DeviceTypeSpecVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    void saveDetail(DeviceTypeSpecForm specForm){
        if (CollectionUtils.isNotEmpty(specForm.getDetailList())){
            for (DeviceTypeSpecValueForm productSpecValueForm:
                    specForm.getDetailList()) {
                DeviceTypeSpecValue specValue=new DeviceTypeSpecValue();
                BeanUtils.copyProperties(productSpecValueForm,specValue);
                specValue.setProductSpecId(specForm.getId());
                deviceTypeSpecValueService.save(specValue);
            }
        }
    }

    @Override
    @Transactional
    public int update(DeviceTypeSpecForm productSpecForm) {
        DeviceTypeSpec productSpec=new DeviceTypeSpec();
        BeanUtils.copyProperties(productSpecForm,productSpec);
        int updateCount=baseMapper.updateById(productSpec);
        deviceTypeSpecValueService.remove(new QueryWrapper<DeviceTypeSpecValue>().eq("product_spec_id",productSpecForm.getId()));
        saveDetail(productSpecForm);
        return updateCount;
    }
}
